﻿using System;
using Xomega.Framework;
using Xomega.Framework.Services;

namespace AdventureWorks.Entities
{
    // Complements the generated Empoloyee entity
    public partial class Employee : IValidatable
    {
        public void Validate(bool force)
        {
            // Validate employee's age and report an error using an error code,
            // which is also a key for a localized message from the resource file.
            // Examples of the resulting message are shown in the comments.
            if (BirthDate > DateTime.Today.AddYears(-18))
            {
                // Invalid new employee. Employee cannot be younger than 18 years old.
                // Invalid employee 123. Employee cannot be younger than 18 years old.
                ErrorList.Current.AddError("EMP_TOO_YOUNG", KeyParams());
            }

            // Validate employee's age and report a warning using a message code,
            // which is also a key for a localized message from the resource file.
            // Examples of the resulting message are shown in the comments.
            if (BirthDate > DateTime.Today.AddYears(-20) && MaritalStatus != "S")
            {
                // Please confirm the marital status of the new employee, who is younger than 20 years old.
                // Please confirm the marital status of the employee 123, who is younger than 20 years old.
                ErrorList.Current.AddWarning("EMP_TOO_YOUNG_TO_MARRY", KeyParams());
            }
        }

        // Utility method to return employee key for existing employees
        // or a word 'new' for substitution into the error messages as parameters.
        public object[] KeyParams()
        {
            if (EntityKey.IsTemporary) return new string[] { Resources.NEW, "" };
            else return new string[] { "", " " + EmployeeId };
        }
    }
}